unit StarSerial;

interface

Uses
  Classes, SysUtils, SynaSer;


function PrintLogo ( logo: integer ) : integer;
function ConnectPrinter(port: string): integer;
function PrintReceiptLine( data: string ): integer;
function StringOfChar( c: char; cpi: integer ): string; // returns full width string in specified cpi
procedure AlignInString( var mainstring: string; substring: string; alignment: TAlignment );
procedure OpenDrawer;

//procedure testserial;

var
  SerialPrinter: TBlockSerial;

implementation

{procedure testserial;
var
 CommPort: string;
 hCommFile:THandle;
 NumWritten, numread:dword;
 toSend:string;
 timeoutbuffer:PCommTimeouts;
 buffer: array[0..255] of char;
begin

  setlength(tosend, 3);
  tosend[1] := #27;
  tosend[2] := #6;
  tosend[3] := #1;
  commport := 'COM1';
  hcommfile :=  Createfile(PChar(CommPort),
                GENERIC_WRITE or GENERIC_READ,
                0,
                nil,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                0);
  Getmem(Timeoutbuffer, Sizeof(TCOMMTIMEOUTS));
  GetCommTimeouts(hcommfile, Timeoutbuffer^);
  timeoutbuffer.ReadIntervalTimeout := 0;
  timeoutbuffer.ReadTotalTimeoutMultiplier :=0;
  timeoutbuffer.ReadTotalTimeoutConstant := 1500;
  SetCommTimeouts(hcommfile, Timeoutbuffer^);
  Freemem(Timeoutbuffer, Sizeof(TCOMMTIMEOUTS));
  writefile(hcommfile,
            PChar(tosend)^,
            3,
            Numwritten,
            nil);
  if readfile( hcommfile,
               buffer,
               10,
               numread,
               nil) = false
               then Showmessage('can not read')
               else showmessage(inttostr(numread));
  CloseHandle(hcommfile);
end;      }

function PrintLogo ( logo: integer ) : integer;
begin
  if SerialPrinter <> nil then
     SerialPrinter.Sendstring(#27#28#112#1#48);
end;

function ConnectPrinter(port: string): integer;
begin
  if SerialPrinter <> nil then SerialPrinter.Free;
  SerialPrinter := TBlockSerial.Create;
  SerialPrinter.Connect(port);
  SerialPrinter.Config(9600,8,'N',SB1,false, true);
  SerialPrinter.SendString(#27#29#116#1);  //set codepage 437
  SerialPrinter.SendString(#27#82#51);  //set UK char set
  SerialPrinter.SendString(#27#68#42#0 );  //set tab
  Result := SerialPrinter.LastError;
end;

function PrintReceiptLine( data: string ): integer;
begin
  SerialPrinter.SendString( data + #10 );
end;

function StringOfChar( c: char; cpi: integer ): string; // returns full width string in specified cpi
var
  strlen: integer;
  x : integer;
  strchars: string;
begin
 Result := '';
  case cpi of
       7: strlen := 19;
       8: strlen := 22;
       14: strlen := 38;
       16: strlen := 44;
       17: strlen := 48;
       end;
  SetLength( strchars, strlen);
  for x := 1 to strlen do
      strchars[x] := c;
  Result := strchars;
end;

procedure AlignInString( var mainstring: string; substring: string; alignment: TAlignment );
var
  startpos: integer;
  x : integer;
begin
  case alignment of
       taLeftJustify : startpos := 0;
       taRightJustify : startpos := Length(mainstring)-Length(substring);
       taCenter : startpos := (length(mainstring)-Length(substring)) div 2;
       end;
  for x := 1 to Length(substring) do
                                    mainstring[x+startpos] := substring[x];
end;

procedure OpenDrawer;
begin
  if SerialPrinter <> nil then
     begin
       SerialPrinter.SendString(#27'='#1#7);
     end;
  SerialPrinter.Flush;
end;


end.